export const initMsgData = async (vm) => {

    try {

        // 使用 await 等待 uni.getStorage 异步操作完成
		if(vm.toTarget.targetId){
			await uni.getStorage({
            key:`msg_${vm.toTarget.targetId}`,
			success(memoryMsg) {
			
			  if (memoryMsg.errMsg === 'getStorage:ok') {
            // 直接使用 memoryMsg.data 获取存储的数据
            vm.messages = memoryMsg.data;
			
        }	
				
			},
			fail(e) {
				console.log("e: " + JSON.stringify(e));
				uni.setStorage({
					key:`msg_${vm.toTarget.targetId}`,
					data:[]
				})
			}
			
        });}
   
           // 检查操作是否成功
      
        const maxTimestamps = {};
        vm.messages.forEach((record) => {
            const { toTarget, timestamp } = record;
            if (!maxTimestamps[toTarget.targetType] || timestamp > maxTimestamps[toTarget.targetType]) {
                maxTimestamps[toTarget.targetType] = timestamp;
            }
        });
        const lastTimes = {
            1: new Date().getTime(),
            2: new Date().getTime(),
            3: new Date().getTime()
        };
        vm.lastTimes = {
            ...lastTimes,
            ...maxTimestamps
        };
        return vm.lastTimes;
    } catch (error) {
        console.error('初始化消息数据失败:', error);
    } finally{

	}
};